import {Caption} from "./Caption";

class Tm{
  open = false
  colors=["#86c5cf","#d83d35","#fdfdf9","#e1ab48","#f2ee5a"]
  canvasTimer = null
  capObjs = []
  ctx = null
  cavWidth = 300
  cavHeight = 100

  constructor(ctx ,width,height){
    this.ctx = ctx
    this.cavWidth = width
    this.cavHeight = height
    //this.initCanvas()
  }

  initCanvas () {
    if (this.canvasTimer == null ) {
      let self = this
      this.canvasTimer = setInterval(function (argument) {
        self.drawAllText()
        self.refreshObjs(self.capObjs)
      },10)
    }

  }

  closeTm =function(){
    //console.log("close tm")
    this.open = false
    this.ctx.clearRect( 0 , 0 , this.cavWidth , this.cavHeight)
    clearInterval(this.canvasTimer)
    this.canvasTimer = null
    this.capObjs.splice(0,this.capObjs.length)
  }

  openTm =function(){
    //console.log("open tm")
    this.open = true
    for(var i = 0 ,len = Caption.topObjs.length ; i < len ; i++){
      Caption.topObjs[i].blank = true
    }
    this.initCanvas()
  }

  sendTmMsg=function(msg){
    //console.log("send msg")
    if(this.open){
      let temcolor = this.colors[this.randomNum(0,4)]
      let temcap = new Caption (  true , 1 , temcolor , msg ,this.cavWidth)
      this.capObjs.push(temcap)
    }
  }
  drawAllText () {
    this.ctx.clearRect( 0 , 0 , this.cavWidth , this.cavHeight)
    this.ctx.beginPath()
    let _this = this
    for(var i=0 , len = this.capObjs . length ; i < len ; i++ ){
      if(this.capObjs[i] == null || this.capObjs[i].isOver == undefined)
      {
        //console.log("data is remove")
        continue
      }
      //console.log("drawAllText ",this.capObjs[i].top)
      if(this.capObjs[i].isOver)
      {
        let tmpC = _this.capObjs[i].content
        this.capObjs[i] = null;
        this.capObjs.splice(i , 1)
        //console.log(tmpC," is over")
        setTimeout(()=>{
          _this.sendTmMsg(tmpC);
        },100)

        continue
      }
      this.ctx.lineWidth = 0.2;
//    this.ctx.fillStyle = "#fdfdf9";
      this.ctx.font = "0.9em Arial,San Francisco,Helvetica,SimHei,SimSun,sans-serif";
//    this.ctx.fillText( this.capObjs[i].content , this.capObjs[i].left , this.capObjs[i].top );
      
      
      this.ctx.strokeStyle = "#c4c1b5";
      this.ctx.strokeText(this.capObjs[i].content , this.capObjs[i].left , this.capObjs[i].top);
      this.ctx.fillStyle = "#f4f3ef";
      this.ctx.fillText( this.capObjs[i].content , this.capObjs[i].left , this.capObjs[i].top );
      
      
      
      this.ctx.closePath();
      this.capObjs[i].moving()
    }
  }

  refreshObjs(objs) {
    for (var i = objs.length - 1; i >= 0; i--) {
      //console.log("objs[i]",i,this.capObjs[i].left,objs[i].latestTime)
      if (objs[i].left < -this.cavWidth*0.6 || objs[i].latestTime > 450 ) {
        objs.splice(i , 1)
      }

    }
  }

  randomNum(minNum,maxNum){
    switch(arguments.length){
      case 1:
        return parseInt(Math.random()*minNum+1,10);
        break;
      case 2:
        return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
        break;
      default:
        return 0;
        break;
    }
  }

}

export {Tm}
